<template>
  <div>
    <div style="margin: 10px 0;">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-plus"></i></el-button>
      <el-popconfirm
          style="margin-left: 10px"
          confirmButtonText="确定"
          cancelButtonText="取消"
          title="确定删除吗？"
          @confirm="handleDeleteAll"
      >
        <el-button type="danger" slot="reference">批量删除
          <i class="el-icon-delete"></i></el-button>
      </el-popconfirm>
    </div>
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg" row-key="id" default-expand-all
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index">
      </el-table-column>
      <el-table-column prop="name" label="名称">
      </el-table-column>
      <el-table-column prop="path" label="路径">
      </el-table-column>
      <el-table-column prop="pagePath" label="页面路径">
      </el-table-column>
      <el-table-column prop="icon" label="图标" align="center">
        <template slot-scope="scope">
          <i :class="scope.row.icon" style="font-size: 20px"></i>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述">
      </el-table-column>
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <!--    以当前目录结构，只有一级菜单和路径不存在，才能创建子菜单比较合理  -->
          <el-button type="primary" @click="addChildMenu(scope.row.id)" v-if="scope.row.pid == 0 && !scope.row.path">
            新增子菜单<i class="el-icon-menu"></i>
          </el-button>
          <el-button type="success" @click="handleEdit(scope.$index, scope.row)">编辑<i class="el-icon-edit"></i>
          </el-button>
          <el-popconfirm
              style="margin-left: 10px"
              confirmButtonText="确定"
              cancelButtonText="取消"
              title="确定删除吗？"
              @confirm="handleDelete(scope.$index, scope.row)"
          >
            <el-button type="danger" slot="reference">删除
              <i class="el-icon-delete"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="角色信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="form.path" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="页面路径">
          <el-input v-model="form.pagePath" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-select v-model="form.icon" placeholder="请选择" style="width: 100%">
            <template slot="prefix">
              <i :class="form.icon" style="font-size: 16px"></i>
            </template>
            <el-option
                v-for="item in options"
                :key="item.name"
                :label="item.name"
                :value="item.value">
              <!--  展示图标和名字 -->
              <i :class="item.value" style="margin-right: 5px"></i>
              <span>{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      tableData: [],
      // 名称
      name: "",
      // 描述
      description: "",
      // 表单数据
      form: {},
      // 当前页码
      pageNum: 1,
      // 每页条数
      pageSize: 10,
      // 总条数
      total: 10,
      // 表格选中数据
      multipleSelection: [],
      // 表单数据,是否展示
      dialogFormVisible: false,
      // 菜单样式
      headerBg: 'headerBg',
      options: [],
    }
  },
  methods: {
    load() {
      request.post("/menu/getMenuTree").then(res => {
        if (res.code === 200) {
          this.tableData = res.data;
        }
      })
    },
    reset() {
      this.username = "";
      this.email = "";
      this.address = "";
      this.load();
    },
    // 分页
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    },
    // 分页
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    // 编辑
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.form = JSON.parse(JSON.stringify(row));
      request.post("/menu/getIcons").then(res => {
        if (res.code === 200) {
          this.options = res.data;
        }
      })
    },
    // 删除
    handleDelete(index, row) {
      request.delete("/menu/" + row.id).then(res => {
        this.load();
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }
      })
    },
    // 批量删除
    handleDeleteAll() {
      let ids = [];
      this.multipleSelection.forEach(item => {
        ids.push(item.id);
      })
      request.post("/menu/delBatch", ids).then(res => {
        this.load();
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      })
    },
    // 控制新增弹窗
    handleAdd() {
      this.dialogFormVisible = true;
      this.form = {
        username: "",
        nickname: "",
        email: "",
        phone: "",
        address: ""
      }

    },
    // 保存
    saveUser() {
      request.post("/menu/saveOrUpdate", this.form).then(res => {
        if (res.code === 200) {
          this.load();
          this.$message({
            type: 'success',
            message: '保存成功!'
          });
        } else {
          this.$message({
            type: 'error',
            message: '保存成功'
          });
        }
        this.dialogFormVisible = false;
      })
    },
    // 列表多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    addChildMenu(parentId) {
      this.dialogFormVisible = true;
      this.form = {}
      if (parentId) {
        this.form.pid = parentId
      }

      request.post("/menu/getIcons").then(res => {
        if (res.code === 200) {
          this.options = res.data;
        }
      })

    }
  },
  mounted() {
    this.load();
  }
}
</script>

<style scoped>
.headerBg {
  background-color: #eeeeee !important;
}
</style>